﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using LBSWebService.BusinessLogic.BusinessObjects;
using System.Collections.Generic;

namespace LBSWebService.DataAccess
{
    /// <summary>
    /// Class for accessing the linq based datacontext
    /// </summary>
    public static class LBSDataAccessLayer
    {
        static LBSDataAccessLayer()
        {
            LBSDataDataContext lbsDataContext = new LBSDataDataContext();

            if (!lbsDataContext.DatabaseExists())
            {
                lbsDataContext.CreateDatabase();
            }
        }

        /// <summary>
        /// Returns the last stored location info for the given user.
        /// </summary>
        /// <param name="pUserId">Guid UserId</param>
        /// <returns>Database entity</returns>
        public static CurrentLocation GetCurrentLocation(Guid pUserId)
        {
            try
            {
                LBSDataDataContext lbsDataContext = new LBSDataDataContext();

                var q = from p in lbsDataContext.CurrentLocations
                        where p.UserId == pUserId
                        select p;
                return q.First();

            }
            catch (Exception)
            {
                return null;
            }

        }

        /// <summary>
        /// Gets a UserProfile by UserId
        /// </summary>
        /// <param name="pUserId"></param>
        /// <returns></returns>
        public static UserProfile GetUserProfile(Guid pUserId)
        {
            LBSDataDataContext lbsDataContext = new LBSDataDataContext();
            var q = from p in lbsDataContext.UserProfiles
                    where p.UserId == pUserId
                    select p;
            return q.First();
        }

        /// <summary>
        /// Updates the location info for the given user
        /// </summary>
        /// <param name="pLocObject">Object is comming from a device. This method converts
        /// this object to a data entity</param>
        /// <param name="pUserId">Guid UserId represents an unique user</param>
        public static void UpdateLocationObjectPool(LocationObject pLocObject, Guid pUserId)
        {

            try
            {
                LBSDataDataContext lbsDataContext = new LBSDataDataContext();


                var usrQ = from u in lbsDataContext.UserItems
                           where u.UserId == pUserId
                           select u;

                if (usrQ.Count() != 0)
                {

                    var objToUpdate = from cl in lbsDataContext.CurrentLocations
                                      where cl.UserId == pUserId
                                      select cl;



                    CurrentLocation currLocObjToUpdate = new CurrentLocation();

                    if (objToUpdate.Count() != 0)
                    {
                        currLocObjToUpdate = objToUpdate.First();
                        currLocObjToUpdate.Latitude = pLocObject.Latitude;
                        currLocObjToUpdate.Longitude = pLocObject.Longitude;
                        currLocObjToUpdate.LastUpdated = DateTime.Now;
                    }
                    else
                    {
                        currLocObjToUpdate.Latitude = pLocObject.Latitude;
                        currLocObjToUpdate.Longitude = pLocObject.Longitude;
                        currLocObjToUpdate.LastUpdated = DateTime.Now;
                        currLocObjToUpdate.UserItem = usrQ.First();

                        lbsDataContext.CurrentLocations.InsertOnSubmit(currLocObjToUpdate);
                    }

                    lbsDataContext.SubmitChanges();
                }
            }
            catch (Exception)
            {


            }

        }

        public static List<CurrentLocation> GetAllCurrentLocation(DateTime pLastUpdated)
        {
            LBSDataDataContext lbsDataContext = new LBSDataDataContext();
            var q = from p in lbsDataContext.CurrentLocations
                    where p.LastUpdated >= pLastUpdated
                    select p;
            return q.ToList();
        }

    }
}
